//
//  CountryViewController.m
//  NiuBanJinGold
//
//  Created by Billy on 15/11/12.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "CountryViewController.h"
#import "Country.h"
#import "RollOutViewController.h"

@interface CountryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView; //数据源显示
@property (nonatomic, strong) NSMutableArray *mutableArray_allCountries;//所有县

@end

@implementation CountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
     [self.navigationItem setTitle:@"选择县"];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self getDataFromServer];
}

#pragma mark - data preparation

- (void) getDataFromServer {
    
    NSDictionary *dic_params = @{@"userId":GETCURRENTUSERINOBYPRAMETERES(user_userId),@"token":GETCURRENTUSERINOBYPRAMETERES(user_userToken),@"cityId":self.str_id};
    
    [[NetworkUtil sharedNetworkUtil] zzjrTrade_transfer_get_countyByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
        
        
        if (!error) {
            
            [self.mutableArray_allCountries removeAllObjects]; // remove dupe
            
            NSDictionary *dic = parsedData;
            
            if ([dic[@"status"] integerValue] == 0) {
                
                for (NSDictionary *dic_element in dic[@"result"]) {
                    
                    Country *tmp = [[Country alloc] initWithAttributes:dic_element];
                    [self.mutableArray_allCountries addObject:tmp];
                }
            }
            
            if ([self.mutableArray_allCountries count] > 0) {
                
                [self.tableView reloadData];
            }
            
        }else {
            
            [UsefulUtil showAlertView:@"遇到网络问题，请稍候重试!"];
        }
        
    }];
}

#pragma mark - TableView datasource && delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.mutableArray_allCountries count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifer = @"cell" ;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];
    }
    
    Country *tmp = [self.mutableArray_allCountries objectAtIndex:indexPath.row];
    
    [cell.textLabel setText:tmp.country_name];
    
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Country *tmp = [self.mutableArray_allCountries objectAtIndex:indexPath.row];
    
    [UsefulUtil saveCustomObject:tmp intoUserDefaultByKey:@"country"];

    for (UIViewController *tmp in self.navigationController.viewControllers) {
        
        if ([tmp isMemberOfClass:[RollOutViewController class]]) {
            
            [self.navigationController popToViewController:tmp animated:YES];
        }
    }
}

#pragma mark - setter method

- (NSMutableArray *)mutableArray_allCountries {
    
    if (!_mutableArray_allCountries) {
        
        _mutableArray_allCountries = [NSMutableArray array];
    }
    return _mutableArray_allCountries;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    }
    
    return _tableView;
}

@end
